Repository navigation
Introduce bytecode caching, polish "cjs" bundler output format - #14232
Conversation
"cjs" bundler output format
"cjs" bundler output format"cjs" bundler output format
| target: "node", | ||
| run: { | ||
| stdout: "undefined undefined", | ||
| stdout: "true true", |
There was a problem hiding this comment.
ooh i like this. import.meta should just work, aliasing to __dirname and __filename.
this test however should
- run in node to ensure
import.metais not actually emitted. you can do this by settingruntime: "node"in the run object. - fs.existsSync is not a good enough behavior test. what if the path is
/. it should be a string that is checked for exactly. if you turn the config object passed to itBundled into a function with signature(({ root }) => ({ files: ...you can use that path to write exact expectations for the absolute paths. seetsconfig/Pathfor more details on this api.
though after irl mention of this, it seems we are emitting import.meta, so maybe we just make this behavior error since that is likely easier.
| }, | ||
| target: "node", | ||
| format: "cjs", | ||
| bundleErrors: { |
| // format: "cjs", | ||
| // platform: "node", | ||
| // }); | ||
| itBundled("default/RequireMainCacheCommonJS", { |
There was a problem hiding this comment.
@Paperdave we fixed this since that test was commented out, pretty sure
| format: "iife", | ||
| }); | ||
| itBundled("importstar/ExportSelfIIFEWithName", { | ||
| todo: true, |
There was a problem hiding this comment.
i'm scoping out support for IIFE in this PR
"cjs" bundler output format"cjs" bundler output format
| // If we can pull out a ModuleNamespaceObject, let's do it. | ||
| if (esm?.evaluated && (esm.state ?? 0) >= $ModuleReady) { | ||
| const namespace = Loader.getModuleNamespaceObject(esm!.module); | ||
| return (mod.exports = |
There was a problem hiding this comment.
cc @Paperdave @dylan-conway please take a look at this
There was a problem hiding this comment.
what is the reasoning behind this change. it is breaking.
There was a problem hiding this comment.
Discussed IRL. The goal of this, originally, was to let you mutate values on the ES module namespace object. But, that doesn't work and has not worked.
Instead, this has the effect of making console.log and (...) not work as well
| return promise; | ||
| } else { | ||
| const promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.init(this.main)) orelse return error.JSError; | ||
| const promise = JSModuleLoader.loadAndEvaluateModule(this.global, &String.fromBytes(this.main)) orelse return error.JSError; |
There was a problem hiding this comment.
This change looks identical to the original
| .already_bundled = true, | ||
| .hash = 0, | ||
| .bytecode_cache = if (bytecode_slice.len > 0) bytecode_slice.ptr else null, | ||
| .bytecode_cache_size = if (bytecode_slice.len > 0) bytecode_slice.len else 0, |
There was a problem hiding this comment.
| .bytecode_cache_size = if (bytecode_slice.len > 0) bytecode_slice.len else 0, | |
| .bytecode_cache_size = bytecode_slice.len, |
| // If we can pull out a ModuleNamespaceObject, let's do it. | ||
| if (esm?.evaluated && (esm.state ?? 0) >= $ModuleReady) { | ||
| const namespace = Loader.getModuleNamespaceObject(esm!.module); | ||
| return (mod.exports = |
There was a problem hiding this comment.
what is the reasoning behind this change. it is breaking.
| target: "node", | ||
| run: { | ||
| stdout: "undefined undefined", | ||
| stdout: "true true", |
There was a problem hiding this comment.
ooh i like this. import.meta should just work, aliasing to __dirname and __filename.
this test however should
- run in node to ensure
import.metais not actually emitted. you can do this by settingruntime: "node"in the run object. - fs.existsSync is not a good enough behavior test. what if the path is
/. it should be a string that is checked for exactly. if you turn the config object passed to itBundled into a function with signature(({ root }) => ({ files: ...you can use that path to write exact expectations for the absolute paths. seetsconfig/Pathfor more details on this api.
though after irl mention of this, it seems we are emitting import.meta, so maybe we just make this behavior error since that is likely easier.
| }, | ||
| target: "node", | ||
| format: "cjs", | ||
| bundleErrors: { |
| // format: "cjs", | ||
| // platform: "node", | ||
| // }); | ||
| itBundled("default/RequireMainCacheCommonJS", { |
| format: "iife", | ||
| }); | ||
| itBundled("importstar/ExportSelfIIFEWithName", { | ||
| todo: true, |
Co-authored-by: Dylan Conway <35280289+dylan-conway@users.noreply.github.com>
What does this PR do?
This adds a new
--bytecodeflag, and// @bun @bun-cjscomment that marks a file as already transpiled with CommonJS.2x faster
tsc --helpWhen you use
--bytecodewithout--compile, it adds.jscfiles which contain the compiled bytecode. When the bytecode is unable to be loaded, it uses the source code. Bytecode compilation currently has no impact on async functions and no impact on ESM.How did you verify your code works?
Added tests.